package api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.SneakyThrows;
import model.Article;
import model.UserInfo;
import util.DBUtils;
import util.ResultUtils;
import util.SessionUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wenjingyuan
 * @Date: 2023/03/27/19:54
 * @Description:查看我的文章列表
 */
@WebServlet("/myartlist")
public class MyArticleListServlet extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserInfo userInfo = SessionUtils.getLogInUser(req);
        List<Article> list = new ArrayList<>();
        String msg = "未知错误！";
        if (userInfo != null) {
            int uid = userInfo.getUid();
            if (uid > 0) {
                msg = "成功";
                //查询数据库得到文章的列表
                //查询数据库的五步
                //1.得到connection
                Connection connection = DBUtils.getConnection();
                //2.拼接sql 得到执行器
                String sql = "select * from article where uid=?";
                PreparedStatement statement = connection.prepareStatement(sql);
                //3.设置占位符 并执行sql
                statement.setInt(1, uid);
                ResultSet resultSet = statement.executeQuery();
                //4.将查询的结果添加到容器中
                while (resultSet.next()) {
                    String context = resultSet.getString("context");
                    Article article = new Article();
                    article.setAid(resultSet.getInt("aid"));
                    article.setTitle(resultSet.getString("title"));
                    article.setContext(resultSet.getString("context"));
                    //简介
                    int subLength = Integer.min(20, context.length());
                    article.setDesc(context.substring(0, subLength).replace("#", ""));
                    String createtime = resultSet.getString("createtime");
                    article.setCreatetime(createtime.substring(0, createtime.lastIndexOf(".")));
                    article.setUpdatetime(resultSet.getString("updatetime"));
                    article.setReadcount(resultSet.getInt("readcount"));
                    article.setUid(resultSet.getInt("uid"));
                    article.setState(resultSet.getInt("state"));
                    list.add(article);
                }
                msg = "成功";
                DBUtils.close(resultSet, statement, connection);
            } else {
                msg = "非法参数！";
            }
            }else{
            msg="未登录";
        }
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().println(JSONUtil.toJsonStr(ResultUtils.toResult(
                    200, list, msg
            )));

    }
}
